// pages/homepage/homepage.js
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		leftList: [],
		rightList: [],
		leftHight: 0,
		rightHight: 0,

		leftList2: [],
		rightList2: [],
		leftHight2: 0,
		rightHight2: 0,
		//首页banner
		bannerList: [],
		//我们的优势
		advantageList: [],
		goAdvantageID: 0,
		//优秀案例
		casesList: [],
		//产品列表
		productList: [],
		// 畅销商品
		start: 0,
		page: 1,
		size: 6,
		allNum: 0,
		isEnd: false,
		// 合作品牌
		coopList: [],
		// 新闻
		newsList: [],
	},


	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		var thatAll = this;
		var url_ = getApp().globalData.url_;
		// 首页  获取banner
		wx.request({
			url: url_ + '/api/banner/index',
			method: "GET",
			dataType: "json",
			success: function(res) {
				// console.log(res.data.data)
				var imgList = res.data.data;
				for (var i = 0; i < imgList.length; i++) {
					var img = url_ + imgList[i].image;
					imgList[i].image = img;
				}
				thatAll.setData({
					bannerList: res.data.data,
				})
			},
			fail: function(e) {
				console.log("请求失败");
			},
		})
		//我们的优势
		wx.request({
			url: url_ + '/api/advantage',
			method: "GET",
			dataType: "json",
			success: function(res) {
				var imgList = res.data.data;
				for (var i = 0; i < imgList.length; i++) {
					var img = url_ + imgList[i].image;
					imgList[i].image = img;
				}
				thatAll.setData({
					advantageList: res.data.data,
				})
			},
			fail: function(e) {
				console.log("请求失败");
			},
		})
		//新闻资讯 
		wx.request({
			url: url_ + '/api/news',
			method: "GET",
			dataType: "json",
			header: {
				'content-type': 'application/json' // 默认值
			},
			success: function(res) {
				thatAll.setData({
					newsList: res.data.data,
				})
			},
			fail: function(e) {
				console.log("请求失败");
			},
		})
		//优秀案例
		wx.request({
			url: url_ + '/api/excellent_cases',
			method: "GET",
			dataType: "json",
			success: function(res) {
				var imgList = res.data.data;
				for (var i = 0; i < imgList.length; i++) {
					var img = url_ + imgList[i].image;
					imgList[i].image = img;
				}
				thatAll.setData({
					casesList: res.data.data,
				})
			},
			fail: function(e) {
				console.log("请求失败");
			},
		})
		//获取产品数目
		this.proNum();
		//产品列表
		this.getProData(this.data.leftHight2,
			this.data.rightHight2,
			this.data.leftList2,
			this.data.rightList2)

		//合作商家
		wx.request({
			url: url_ + '/api/coop_business',
			method: "GET",
			dataType: "json",
			success: function(res) {
				var allData = res.data.data;
				// if(allData.length != 0){
				//定义两个临时的变量来记录左右两栏的高度，避免频繁调用setData方法
				// var leftH = thatAll.data.leftHight;
				// var rightH = thatAll.data.rightHight;
				var leftData = [];
				var rightData = [];
				var CoverWidth = 0;
				var CoverHeight = 0;
				for (let i = 0; i < allData.length; i++) {
					var img = url_ + allData[i].image;
					allData[i].image = img;
					if (!(i % 2)) { //判断左右两侧当前的累计高度，来确定item应该放置在左边还是右边
						leftData.push(allData[i]);
						// leftH += currentItemHeight;
					} else {
						rightData.push(allData[i]);
						// rightH += currentItemHeight;
					}
				}
				thatAll.setData({
					coopList: res.data,
					//合作品牌
					// leftHight: leftH,
					// rightHight: rightH,
					leftList: leftData,
					rightList: rightData
				})
				// }
				//更新左右两栏的数据以及累计高度
			},
			fail: function(e) {
				console.log("请求失败");
			},
		})
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {
		//获取更多
		this.data.start = this.data.page * this.data.size;
		this.getProData(this.data.leftHight2,
			this.data.rightHight2,
			this.data.leftList2,
			this.data.rightList2);

		this.data.page += 1;
		if (this.data.allNum < this.data.page * this.data.size) {
			this.setData({
				isEnd: true
			})
		}
	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	},
	//产品数目
	proNum: function() {
		var that = this;
		var url_ = getApp().globalData.url_;
		wx.request({
			url: url_ + '/api/product/total',
			method: "GET",
			dataType: "json",
			success: function(res) {
				that.setData({
					allNum: res.data.data.total,
				})
			},
			fail: function(e) {
				console.log("请求失败");
			},
		})
	},
	//获取商品列表
	getProData: function(lastLH, lastRH, lastLeft, lastRight) {
		var that = this;
		var url_ = getApp().globalData.url_;
		if (this.data.isEnd) {
			return
		}

		wx.request({
			url: url_ + '/api/product?start='+ that.data.start +'&count='+ that.data.size,
			method: "GET",
			dataType: "json",
			success: function(res) {
				//畅销商品
				var allData2 = res.data.data;
				//定义两个临时的变量来记录左右两栏的高度，避免频繁调用setData方法
				var leftH2 = lastLH;
				var rightH2 = lastRH;
				var leftData2 = [];
				var rightData2 = [];
				var CoverWidth2 = 0;
				var CoverHeight2 = 0;

				for (let j = 0; j < allData2.length; j++) {
					var img = url_ + allData2[j].cover_image;
					allData2[j].cover_image = img;
					CoverWidth2 = parseInt(allData2[j].imagewidth)
					CoverHeight2 = parseInt(allData2[j].imageheight);
					var currentItemHeight2 = parseInt((CoverHeight2 * 375) / CoverWidth2);
					if (leftH2 == rightH2 || leftH2 <
						rightH2) { //判断左右两侧当前的累计高度，来确定item应该放置在左边还是右边
						leftData2.push(allData2[j]);
						leftH2 += currentItemHeight2;
					} else {
						rightData2.push(allData2[j]);
						rightH2 += currentItemHeight2;
					}
				}
				//新旧数据合并
				var lastLeftData = lastLeft;
				var lastRightData = lastRight;
				lastLeftData.push(...leftData2)
				lastRightData.push(...rightData2)
				//畅销商品
				that.setData({
					productList: res.data.data,
					leftHight2: leftH2,
					rightHight2: rightH2,
					leftList2: lastLeftData,
					rightList2: lastRightData
				})


			},
			fail: function(e) {
				console.log("请求失败");
			},
		})


	},
	//我们的优势
	advantageGoDetail: function(e) {
		wx.navigateTo({
			url: "../advantage/advantageDetails?id=" + e.currentTarget.dataset.id
		})
	},
	//优秀案例
	caseGoDetail: function(e) {
		wx.navigateTo({
			url: "../case/caseDetails?id=" + e.currentTarget.dataset.id
		})
	},
	//新闻资讯
	newsGoDetail: function(e) {
		wx.navigateTo({
			url: "../news/newsDetails?id=" + e.currentTarget.dataset.id
		})
	},
	//畅销产品
	prodGoDetail: function(e) {
		wx.navigateTo({
			url: "../product/productDetails?id=" + e.currentTarget.dataset.id
		})
	},
	//banner跳转
	bannerGoDetail: function(e) {
		// console.log(e)
		// product:产品// advantage:我们的优势// case:优秀案例// page:单页
		if (e.currentTarget.dataset.url == 'page') {
			wx.navigateTo({
				url: "../imageText/imageText?id=" + e.currentTarget.dataset.toid
			})

		} else {
			var url1_ = e.currentTarget.dataset.url;
			var toURL = "../" + url1_ + "/" + url1_ + "Details?id=" + e.currentTarget.dataset.toid
			wx.navigateTo({
				url: toURL
			})
		}
	},

})
